/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[PasswordUtils.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class PasswordUtils {

	public static String readPassword(String username) {
		String password = "";

		try {
			String dbuser = "root";
			String dbpass = "123456";
			String driverClass = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql:///jdbc";

			Class.forName(driverClass);
			String sql = "SELECT password FROM Mail_User WHERE username = ?";
			Connection connection = DriverManager.getConnection(url, dbuser, dbpass);
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				password = resultSet.getString("password");
			}
			resultSet.close();
			statement.close();
			connection.close();
		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}

		return password;
	}
}
